package com.ssis.core;

import java.util.ArrayList;
import java.util.HashMap;

import org.newdawn.slick.util.Log;

import com.ssis.core.Cell.CellType;
import com.ssis.core.Crew.CellLocation;
import com.ssis.graphics.Sprite;

public class HUDCrew {
	private Sprite background;
	private Sprite assignments;
	private Sprite[] buttons;
	private CrewCursor crewCursor;
	private AssignmentCursor assignCursor;
	private Game game;
	private ArrayList<Cell> assignmentChoices;
	private HashMap <Integer,CellType[]> assignmentTypes;
	private HashMap <Integer, CellLocation> assignmentTypeButtons;
	public static final int BACKGROUND_X_OFFSET = 100;
	public static final int BACKGROUND_Y_OFFSET = 50;
	public static final int ASSIGNMENTS_X_OFFSET = 120;
	public static final int ASSIGNMENTS_Y_OFFSET = 300;
	public static final int ASSIGNMENT_CHOICE_X_OFFSET = 450;
	public static final int ASSIGNMENT_CHOICE_Y_OFFSET = 100;
	public static final int HIRE_X_OFFSET = 300;
	public static final int HIRE_Y_OFFSET = 300;
	public static final int FIRE_X_OFFSET = 370;
	public static final int FIRE_Y_OFFSET = 300;
	public static final int QUIT_X_OFFSET = 500;
	public static final int QUIT_Y_OFFSET = 300;
	public static final int LIST_X_OFFSET = 300;
	public static final int LIST_Y_OFFSET = 100;
	public static final int LIST_X_TEXT_OFFSET = 7;
	public static final int LIST_Y_TEXT_OFFSET = 7;
	public static final int LIST_LENGTH = 10;
	public static final int STATS_X_OFFSET = 150;
	public static final int STATS_Y_OFFSET = 100;
	public static final int BUTTON_WIDTH = 64;
	public static final int ASSIGNMENT_WIDTH = 32;
	public static final int CELL_X_OFFSET = 400;
	public static final int CELL_Y_OFFSET = 100;
	public static final int LINE_WIDTH = 20;
	public static final int LIST_WIDTH = 100;
	
	public HUDCrew(Game game) {
		this.game = game;
		crewCursor = new CrewCursor();
		assignCursor = new AssignmentCursor();
		background = new Sprite("hud_crew_background", BACKGROUND_X_OFFSET, BACKGROUND_Y_OFFSET);
		assignments = new Sprite("hud_crew_assignments", ASSIGNMENTS_X_OFFSET, ASSIGNMENTS_Y_OFFSET);
		buttons = new Sprite[3];
		buttons[0] = new Sprite("hud_crew_buttons", HIRE_X_OFFSET, HIRE_Y_OFFSET, 1, 4, 4);
		buttons[1] = new Sprite("hud_crew_buttons", FIRE_X_OFFSET, FIRE_Y_OFFSET, 2, 4, 4);
		buttons[2] = new Sprite("hud_crew_buttons", QUIT_X_OFFSET, QUIT_Y_OFFSET, 3, 4, 4);
		initAssignmentTypes();
		getAssignmentChoices();
	}

	public void render() {
		// Not sure how this line affects performance
		getAssignmentChoices();
		
		background.render();
		assignments.render();
		buttons[0].render();
		buttons[1].render();
		buttons[2].render();
		crewCursor.render();
		assignCursor.render();
		for(int i=0; i<game.station().crewSize() && i<LIST_LENGTH; i++) {
			game.text().print(LIST_X_OFFSET + LIST_X_TEXT_OFFSET, LIST_Y_OFFSET + LIST_Y_TEXT_OFFSET + LINE_WIDTH*i, game.station().crew(i).name());
		}
//		Crew selectedCrew = game.station().crew(selectedCrew());
//		if (selectedCrew.getLocation(CellLocation.SLEEP) != null)
//			game.text().print(CELL_X_OFFSET, CELL_Y_OFFSET + LINE_WIDTH * 0, selectedCrew.getLocation(CellLocation.SLEEP).toString());
		for(int i=0; i<assignmentChoices.size() && i<LIST_LENGTH; i++) {
			game.text().print(ASSIGNMENT_CHOICE_X_OFFSET + LIST_X_TEXT_OFFSET, ASSIGNMENT_CHOICE_Y_OFFSET + LIST_Y_TEXT_OFFSET + LINE_WIDTH*i, assignmentChoices.get(i).toString());
		}
		game.text().print(300, 370, "DUDES:  " + game.station().crewSize());
		game.text().print(300, 380, "      / " + game.station().housingCapacity());
		
		if (crewCursor.selected() < game.station().crewSize()) {
			Crew selected = game.station().crew(crewCursor.selected());
			game.text().print(STATS_X_OFFSET, STATS_Y_OFFSET, selected.name());
			game.text().print(STATS_X_OFFSET, STATS_Y_OFFSET + LINE_WIDTH, "Morale: " + selected.morale());
			String statout = "Rest: ";
			if(selected.getLocation(CellLocation.SLEEP) != null)
				statout += selected.getLocation(CellLocation.SLEEP).toString();
			else
				statout += "None";
			game.text().print(STATS_X_OFFSET, STATS_Y_OFFSET + LINE_WIDTH*2, statout);
		}
		
	}
	
	public int click(int x, int y) {
		for(int i=0;i<game.station().crewSize();i++) {
			Log.debug(game.station().crew(i).name());
			Cell c = game.station().crew(i).getLocation(CellLocation.SLEEP);
			if (c!=null)
				Log.debug(c.toString());
		}
		
		if(x > HIRE_X_OFFSET && x < HIRE_X_OFFSET + BUTTON_WIDTH && y > HIRE_Y_OFFSET && y < HIRE_Y_OFFSET + BUTTON_WIDTH)
			return 1; // Hire
		if(x > FIRE_X_OFFSET && x < FIRE_X_OFFSET + BUTTON_WIDTH && y > FIRE_Y_OFFSET && y < FIRE_Y_OFFSET + BUTTON_WIDTH)
			return 2; // Fire
		if(x > QUIT_X_OFFSET && x < QUIT_X_OFFSET + BUTTON_WIDTH && y > QUIT_Y_OFFSET && y < QUIT_Y_OFFSET + BUTTON_WIDTH)
			return 3; // Exit
		if(x > LIST_X_OFFSET && x < LIST_X_OFFSET + LIST_WIDTH && y > LIST_Y_OFFSET && y < LIST_Y_OFFSET + LINE_WIDTH * LIST_LENGTH)
			return 4; // List
		if(x > ASSIGNMENTS_X_OFFSET && x < ASSIGNMENTS_X_OFFSET + ASSIGNMENT_WIDTH * 4 && y > ASSIGNMENTS_Y_OFFSET && y < ASSIGNMENTS_Y_OFFSET + ASSIGNMENT_WIDTH * 2)
			return 5; // Assignments
		if(x > ASSIGNMENT_CHOICE_X_OFFSET && x < ASSIGNMENT_CHOICE_X_OFFSET + LIST_WIDTH && y > ASSIGNMENT_CHOICE_Y_OFFSET && y < ASSIGNMENT_CHOICE_Y_OFFSET + LINE_WIDTH * LIST_LENGTH)
			return 6; // Assignment Options
		return 0;
	}
	
	public int selectedCrew() {
		return crewCursor.selected();
	}

	public void selectCrew(int selected) {
		if(game.station().crewSize() > selected)
			crewCursor.select(selected);
	}
	
	public int selectedAssignment() {
		return assignCursor.selected();
	}

	public void selectAssignmentType(int selected) {
		assignCursor.select(selected);
	}
	
	public void getAssignmentChoices() {
		assignmentChoices = new ArrayList<Cell>();
		CellType[] types = assignmentTypes.get(assignCursor.selected());
		for(CellType type : types)
			assignmentChoices.addAll(game.station().cellsByType(type));
	}
	
	private void initAssignmentTypes() {
		assignmentTypes = new HashMap<Integer, CellType[]>();
		assignmentTypes.put(0, new CellType[] {CellType.DORM, CellType.QUARTERS});
		assignmentTypes.put(1, new CellType[] {CellType.OFFICE});
		assignmentTypes.put(2, new CellType[] {CellType.CARGO});
		assignmentTypes.put(3, new CellType[] {CellType.CARGO});
		assignmentTypes.put(4, new CellType[] {CellType.CARGO});
		assignmentTypes.put(5, new CellType[] {CellType.CARGO});
		assignmentTypes.put(6, new CellType[] {CellType.CARGO});
		assignmentTypes.put(7, new CellType[] {CellType.CARGO});
		
		assignmentTypeButtons = new HashMap<Integer,CellLocation>();
		assignmentTypeButtons.put(0, CellLocation.SLEEP);
		assignmentTypeButtons.put(1, CellLocation.WORK);
		assignmentTypeButtons.put(2, CellLocation.COMBAT);
		assignmentTypeButtons.put(3, CellLocation.EAT);
		assignmentTypeButtons.put(4, CellLocation.ACTIVEREC);
		assignmentTypeButtons.put(5, CellLocation.PASSIVEREC);
		assignmentTypeButtons.put(6, CellLocation.REFRESHER);
		assignmentTypeButtons.put(7, CellLocation.MEDIC);
	}
	
	public void selectAssignment(int number) {
		if(assignmentChoices.size() < number + 1)
			return;
		Crew selectedCrew = game.station().crew(selectedCrew());
		CellLocation selectedCellLocation = assignmentTypeButtons.get(selectedAssignment());
		Cell selectedCell = assignmentChoices.get(number);
		selectedCrew.setLocation(selectedCellLocation, selectedCell);
		
		Log.debug("Assigned crewman " + selectedCrew.name() + " to " + selectedCell);
	}
	
}
